package offer;

import java.util.LinkedList;

public class offer_0032_1 {
    public int[] levelOrder(TreeNode root) {
        LinkedList<TreeNode> list=new LinkedList<>();
        LinkedList<Integer> result=new LinkedList<>();
        if(root==null) return new int[0];
        list.add(root);
        order(list,result);
        while(!list.isEmpty()){
            result.add(list.getFirst().val);
            list.removeFirst();
        }
        int len=result.size();
        int[] results=new int[len];
        for(int i=0;i<len;++i){
            results[i]=result.getFirst();
            result.removeFirst();
        }
        return results;
    }
    public void order(LinkedList<TreeNode> list,LinkedList<Integer> result){
        if(list.isEmpty()) return;
        TreeNode node=list.getFirst();

        result.add(node.val);
        list.removeFirst();
        if(node.left==null&&node.right==null)return;
        if(node.left!=null){
            list.add(node.left);

        }
        if(node.right!=null){
            list.add(node.right);
        }

        order(list,result);
        order(list,result);
    }
}
